<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

/**
 * 工具组事件
 */
Class ToolsEvent extends NewzanAction
{
	/**
	 * 指标可用的名称
	 */
	private $dimension = array(
		'ctr','cpc','cvr','roi','fav_count_rate'
	);

	/**
	 * 淘宝指标名称映射本地指标名称
	 */
	private $allowDimension = array(
		'ctr' => 'ctr',
		'cpc' => 'cpc',
		'cvr' => 'coverage',
		'roi' => 'roi'
	);

	public function getCatsData($parameter)
	{
		
		$category_id = $parameter['category_id'];
		$index = $parameter['index'];
		$form = $parameter['start_date'];
		$to = $parameter['end_date'];
		// 过滤不在维度内的值
		if(!in_array($index, $this->dimension)) {
			return 0;
		}
		$tbIndex = $this->allowDimension[$index];

		$McKey = md5('CatsData_' . $category_id . "{$form}_{$to}");
		S($McKey, null);
		$resData = S($McKey);
		if(!$resData) {
			$rptMdl = new RptModel();
			$resData = $rptMdl->getCatsData($category_id, $form, $to);
			if($resData) {
				$resData = $resData[0];
				switch ($index) {
					case 'cost':
						$resData[$tbIndex] = $resData[$tbIndex] / 100;
						break;
					case 'fav_count_rate':
						$resData['fav_count_rate'] = ($resData['favtotal'] / $resData['click']) * 100;
						break;
					default:
						break;
				}
			}
			$resData[$index] = $resData[$tbIndex];
			// 缓存仅当天有效
            $saveTime = strtotime(date('Y-m-d',strtotime('+1 day')))-time();
			S($McKey, $resData, $saveTime);
		}
		return isset($resData[$index]) ? $resData[$index] : 0;
	}
}
